package hello;

import java.io.Externalizable;

import org.hibernate.Session;
import org.hibernate.Transaction;

import persistence.HibernateUtil;
import auction.chapter08.ItemCompFKToCompNonPK;
import auction.chapter08.UserCompFKToCompNonPK;

public class HelloWorldCompFKToCompNonPK
{
	
	
	public static void main(String[] args)throws Exception
	{
		
		Session s=HibernateUtil.getSessionFactory().openSession();
		Transaction tx=s.beginTransaction();
		
		
		UserCompFKToCompNonPK u=new UserCompFKToCompNonPK();
		u.setFirstName("wei1");
		u.setLastName("sun1");
		
		ItemCompFKToCompNonPK i=new ItemCompFKToCompNonPK();
		i.setTitle("ttt");
		
		u.add(i);
		
		//s.save(u);
		//s.save(i);
		
		tx.commit();
		s.close();
		
		
		Session s1=HibernateUtil.getSessionFactory().openSession();
		Transaction tx1=s1.beginTransaction();
		
		UserCompFKToCompNonPK loadu=(UserCompFKToCompNonPK)s1.load(UserCompFKToCompNonPK.class, 1L);
		out(loadu.getItems().size());
		
		tx1.commit();
		s1.close();
		
		
		
		//HibernateUtil.stat();
		HibernateUtil.shutdown();
	}
	
	public static void getParent(Class clazz)
	{
		Class parent=clazz.getSuperclass();
	  if(parent !=null)
	  {
			System.out.println("###"+parent.getName());
			getParent(parent);
		}
	}
	
	static void out(Object obj)
	{
		System.out.println("---->"+obj);
	}
}
